<template>
    <div>
        <el-container>
            <el-header class="homeHeader">
                <div class="title">医院管理系统</div>
                <el-dropdown @command="commandHandle">
                    <span class="el-dropdown-link" >
                        {{adminUser.roles[0].role}}
                    </span>
                    <el-dropdown-menu slot="dropdown">
<!--                        <el-dropdown-item command="userinfo">个人中心</el-dropdown-item>-->
                        <el-dropdown-item command="logout">注销登录</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </el-header>
            <el-container>
                <el-aside width="200px">
                    <el-menu  router v-for="(item,index) in this.$router.options.routes" :key="index">
                        <el-submenu :index="index+''" v-if="!item.hidden">

                            <template slot="title"><i class="el-icon-s-tools" style="color: #1accff;margin-right: 5px"></i>
                                <span>{{item.name}}</span>
                            </template>
                            <el-menu-item :index="children.path" v-if="!children.hidden" v-for="(children,indexj) in item.children" :key="indexj">
                                {{children.name}}
                            </el-menu-item>

                        </el-submenu>
                    </el-menu>
                </el-aside>

                <el-main><router-view/></el-main>
            </el-container>
        </el-container>
    </div>
</template>

<script>
    export default {
        mounted(){
          this.initAdmin();
        },
        data() {
          return{
             adminUser:{
             }
          }
        },
        methods:{
            menuclick(index){
                this.$router.push(index);
            },
            initAdmin(){
              this.getRequest('/admin/info').then(resp=>{
                if(resp){
                  this.adminUser=resp
                  console.log(this.adminUser)
                  this.getPermission()
                }
              })
            },
            commandHandle(command){
                if(command=='userinfo'){
                    this.$router.push('/userinfo')
                }
                if (command=='logout'){
                    this.$confirm('此操作注销登录, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        //注销登录
                        this.postRequest('/logout');
                        window.sessionStorage.removeItem('tokenStr');
                        this.$router.replace('/');


                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消操作'
                        });
                    });

                }
            },
            getPermission(){ // 模拟接口 获取 权限数据集合
              // 模拟获取的数据 有以下几个权限
              let perms = ['select','view','edit','delete'];
              console.log("====Home====")
              perms.push(this.adminUser.username);
              // 用于把权限集合提交到 actions 中的 SET_PERMISSION 函数
              // 第一个为函数名，后面的参数为 我们需要提交的参数，可以是多个
              this.$store.dispatch("SET_PERMISSION",perms);
          },

        },
      created() {
          // this.getPermission()
      }
    }

</script>

<style>
    .homeHeader{
        background: #409eff;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 15px;
        box-sizing: border-box;
    }
    .homeHeader .title{
        font-size: 30px;
        font-family: 华文行楷;
        color: white;
    }
</style>

